package it.polimi.swKnights.SWIMv2.servlets.users;

import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.friendsEngine.FriendshipManagerRemote;
import it.polimi.swKnights.SWIMv2.ejb.friendsEngine.FriendshipRequestManagerRemote;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class FriendshipRequestController
 */
public class FriendshipRequestController extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

		String destination = "./WEB-INF/friends/FriendPage.jsp";

		try
		{
			if(this.handleError(response, request, request.getParameter("errorr"))){
				return;
			}
			
			this.handleMessage(request);
			this.handleRequests(request);
			this.handleSuggestedFriends(request);
			request.getRequestDispatcher(destination).forward(request, response);
			return;
		}
		catch (LoginFailException e)
		{
			response.sendRedirect("./siteHome?error="+Errors.session);
		}
		catch (InvalidLoginPrivilegesException e)
		{
			response.sendRedirect("./siteHome?error="+Errors.session);
		}
		
	}

	private void handleSuggestedFriends(HttpServletRequest request)
			throws LoginFailException, InvalidLoginPrivilegesException
	{
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");
		FriendshipManagerRemote friendshipRequestManager = this
				.lookUp(FriendshipManagerRemote.class);

		List<Map<String,String>> suggestedFriends = friendshipRequestManager.getSuggestedFriends(
				userName, password);
		request.setAttribute("suggestedFriends", suggestedFriends);
	}

	private void handleRequests(HttpServletRequest request)
			throws LoginFailException, InvalidLoginPrivilegesException
	{
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");
		FriendshipRequestManagerRemote friendshipRequestManager = this
				.lookUp(FriendshipRequestManagerRemote.class);

		List<String> requests = friendshipRequestManager.getFriendshipRequests(
				userName, password);
		request.setAttribute("requests", requests);

	}

	private void handleMessage(HttpServletRequest request)
	{
		String sentTo = request.getParameter("sentTo");
		String added = request.getParameter("added");
		if (sentTo != null)
		{
			request.setAttribute("message",
					"The friendship request has been sent to " + sentTo);
		}
		if (added != null)
		{
			if (added.contains(","))
				request.setAttribute("message", "These users have been added: "
						+ added);
			else
				request.setAttribute("message", "the user " + added
						+ " has been added");
		}
	}

	/**
	 * return true if a redirect has been done
	 * @param response
	 * @param request
	 * @param errorParam
	 * @return
	 * @throws IOException
	 */
	private boolean handleError(HttpServletResponse response,
			HttpServletRequest request, String errorParam) throws IOException
	{
		
		if (errorParam == null || "".equals(errorParam)){
			return false;
		}
		else 
		{
			Errors error = Errors.valueOf(errorParam);
			switch (error)
			{
			case notfound:
				request.setAttribute("error", Errors.notfound);
				return false;
			case session:
				response.sendRedirect("./siteHome?error="+Errors.session);	
				return true;
			case selffriend:
				request.setAttribute("error", Errors.selffriend);
				return false;
			case yetasked:
				request.setAttribute("error", Errors.yetasked);
				return false;
			default:
				return false;

			}
		}
		
	}

}
